import { app, dialog, BrowserWindow, OpenDialogSyncOptions } from 'electron';

/**
 * 选择文件夹
 * @param win 窗口对象
 * @param title 标题
 * @param defaultPath 默认路径
 * @param multiple 是否多选
 * @param showHiddenFiles 是否显示隐藏文件
 */
export function selectDirDialog(
	win: BrowserWindow | undefined,
	title?: string,
	defaultPath = app.getPath('desktop'),
	multiple = false,
	showHiddenFiles = true
) {
	// 属性
	const properties: OpenDialogSyncOptions['properties'] = ['openDirectory', 'promptToCreate', 'createDirectory'];

	if (multiple) properties.push('multiSelections');
	if (showHiddenFiles) properties.push('showHiddenFiles');

	const options: OpenDialogSyncOptions = {
		title,
		defaultPath,
		properties,
	};

	if (win) {
		return dialog.showOpenDialogSync(win, options);
	} else {
		return dialog.showOpenDialogSync(options);
	}
}
